package com.wbq.datastructuredemo.tools;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.wbq.datastructuredemo.domain.Basic;

/**
 * 完成对Json数据的解析
 * 
 * @author wbq
 *
 */

public class JsonTools {
	
	public static List<Basic> getBasics(String key,String jsonString){
		List<Basic> list = new ArrayList<Basic>();
		JSONObject jsonObject;
		try {
			jsonObject = new JSONObject(jsonString);
			//返回Json的数组
			JSONArray jsonArray = jsonObject.getJSONArray(key);
			for(int i=0;i<jsonArray.length();i++){
				JSONObject jsonObject2 = jsonArray.getJSONObject(i);
				Basic basic = new Basic();
				basic.setId(jsonObject2.getInt("id"));
				basic.setTitle(jsonObject2.getString("title"));
				basic.setContent(jsonObject2.getString("content"));
				basic.setAnnotation(jsonObject2.getString("annotation"));
				basic.setUser_id(jsonObject2.getInt("user_id"));
				basic.setImportance(jsonObject2.getInt("importance"));
				basic.setShowFlag(jsonObject2.getInt("showFlag"));
				basic.setVersion(jsonObject2.getInt("version"));
				list.add(basic);
			}
		} catch (JSONException e) {
			e.printStackTrace();
		}
		
		return list;
	}
}
